package com.cwd.arm.inter.conf;

import com.cwd.arm.common.enums.ErrorEnum;
import com.cwd.arm.common.exception.ArmException;
import com.cwd.arm.inter.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by yan on 21-11-30.
 */
@Slf4j
@ControllerAdvice
public class ArmExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseVo<Object> errorHandler(Exception e) {
        e.printStackTrace();
        log.warn("请求错误ArmException:{}",e.getMessage(),e);
        if(e instanceof ArmException){
            return ResponseVo.BUILDER().ERROR((ArmException)e);
        }
        if(e instanceof AccessDeniedException){
            return ResponseVo.BUILDER().ERROR(ErrorEnum.AUTH_ERROR,e.getMessage());
        }
        log.error("请求错误:{}",e.getMessage(),e);
        return ResponseVo.BUILDER().ERROR();
    }



}
